# encoding:utf-8
from __future__ import division
import time
import json
import datetime

import requests
from constant import constant_define

from dao.MySqlDAL import MySqlDAL

'''
http://api.money.126.net/data/feed/0600030

_ntes_quote_callback({"0600030":{"code": "0600030", "percent": -0.030942, "price": 13.78, "open": 13.94, "updown": -0.44, "type":
"SH", "symbol": "600030", "yestclose": 14.22, "volume": 90936696,  "name": "\u4e2d\u4fe1\u8bc1\u5238", "turnover": 1260748277} });
'''


class StockPriceCrawler(object):
    __target = ""
    __page_size = 900
    __sqlUtil = MySqlDAL()
    __tablename = 'tb_stock_price'

    __headers = {
        'Host': 'api.money.126.net',
        'Connection': 'keep-alive',
        'Cache-Control': 'max-age=0',
        'Accept': 'text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8',
        'User-Agent': constant_define.user_agent,
        'Accept-Encoding': 'gzip, deflate, sdch',
        'Accept-Language': 'zh-CN,zh;q=0.8,en;q=0.6'
    }


    # 设置目标网址
    def set_target(self, target):
        self.__target = target


    # 将股票代码分组，每组最多900个 [[],[],[]...]
    def get_stock_list(self):
        query_sql = r'SELECT stock_id FROM tb_stock_info'
        res_dict = self.__sqlUtil.get_dimensions_rows(query_sql)
        params = list()
        slen = len(res_dict)
        count = slen//self.__page_size
        for i in xrange(0,count):
            param = res_dict[self.__page_size*i:self.__page_size*(i+1)]
            params.append(param)

        if slen%self.__page_size > 0 :
            param = res_dict[self.__page_size*count:]
            params.append(param)

        return params

    # 处理请求返回json
    def get_request(self,req_url):
        req = requests.get(req_url,headers=self.__headers)
        str = req.content[req.text.index('('):]
        return json.loads(str[1:len(str)-2])


    # 获取并设置网页内容
    def get_stock_info_json(self):
        stock_group = self.get_stock_list()
        stock_json = dict()
        for item in stock_group:
            param = ''
            for row in item:
                if row['stock_id'][0:2] == 'SH':
                    param += '0'+row['stock_id'][2:]+','
                else:
                    param += '1'+row['stock_id'][2:]+','
            param = param[:len(param)-1]
            stock_json.update(self.get_request(self.__target+param))
            # 防止频繁访问网站
            time.sleep(1)
        return stock_json


    # 获取股票流通市值
    def get_market_value(self):
        res_list = self.__sqlUtil.get_dimensions_rows('SELECT stock_id,market_value FROM tb_stock_info')
        stock_list = dict()
        for item in res_list:
            tmp = {item['stock_id']:item['market_value']}
            stock_list.update(tmp)
        return stock_list


    # 写db
    def writeToDB(self):
        mv_dict = self.get_market_value()
        data_keys = ['stock_id','price','percent','open','updown','yestclose','volume','turnover','turnover_rate','date','update_time']
        data_source = []
        for row in self.get_stock_info_json().itervalues():
            tmp = list()
            if 'type' not in row:
                continue
            tmp.append(row['type']+str(row['symbol']))
            tmp.append(row['price'])
            tmp.append(row['percent'])
            tmp.append(row['open'])
            tmp.append(row['updown'])
            tmp.append(row['yestclose'])
            tmp.append(row['volume'])
            tmp.append(row['turnover'])
            if mv_dict[tmp[0]] == 0:
                tmp.append(0)
            else:
                tmp.append(row['turnover']/(mv_dict[tmp[0]]*10**8))
            tmp.append(datetime.date.today())
            tmp.append(time.time())
            data_source.append(tmp)

        return self.__sqlUtil.insert_many_batch(data_keys,data_source,self.__tablename,50)


    # 更新 db
    def updateDB(self):
        mv_dict = self.get_market_value()
        data_keys = ['price','percent','open','updown','yestclose','volume','turnover','turnover_rate','update_time']
        data_source = []
        filter_collection_key = ['stock_id','date']
        for row in self.get_stock_info_json().itervalues():
            tmp = list()
            if 'type' not in row:
                continue
            stock_id = row['type']+str(row['symbol'])
            tmp.append(row['price'])
            tmp.append(row['percent'])
            tmp.append(row['open'])
            tmp.append(row['updown'])
            tmp.append(row['yestclose'])
            tmp.append(row['volume'])
            tmp.append(row['turnover'])
            if mv_dict[stock_id] == 0:
                tmp.append(0)
            else:
                tmp.append(row['turnover']/(mv_dict[stock_id]*10**8))
            tmp.append(time.time())
            tmp.append(stock_id)
            tmp.append(datetime.date.today())
            data_source.append(tmp)

        return self.__sqlUtil.update_many_batch(data_keys,data_source,self.__tablename,filter_collection_key,50)
